<?php
class Training_Form_Group extends Zend_Form{
	
	public function __construct($arrParam = null, $options = null){
		parent::__construct();
		
		
		$tblGroup = new Zendvn_Model_UserGroup();
		$item = $tblGroup->getItem($arrParam);
		
		echo '<pre>';
		print_r($arrParam);
		echo '</pre>';
		
		$this->setMethod('post')
			->setEnctype('multipart/form-data')
			->setAction('')
			->setName('appForm');
		
		//================= name ================
		$name = new Zend_Form_Element_Text('name'); //$name
		$name->setLabel('Group name:')
			->setRequired(true)
				->setDescription('Nhap ten cua nhom');
				
		
		//=================avatar================
		$avatar = new Zend_Form_Element_File('avatar');
		$avatar->setLabel('Avatar:')			
			 ->setDescription('Upload hinh anh dai dien');
		if($arrParam['action'] !='edit'){
			$avatar->setRequired(true);
		}
			 
		//=================ranking================
		$ranking = new Zend_Form_Element_File('ranking');
		$ranking->setLabel('Ranking:')
			 ->setDescription('Upload hinh anh ranking');
		
		//=================group_acp================ 
		$group_acp = new Zend_Form_Element_Checkbox('group_acp');
		$group_acp->setLabel('Admin Control Panel: ')
				 ->setCheckedValue(1)
				 ->setChecked(false)				 
				 ->setDescription('Duoc quyen truy cap vao ACP?');
		
		//================group_default================
		$group_default = new Zend_Form_Element_Radio('group_default');
		$group_default->setLabel("Nhóm mặc định")
				->setValue(0)
				->addMultiOptions(array('No','Yes'));
					 
		//================group_default================
		$status = new Zend_Form_Element_Radio('status');
		$status->setLabel("Trạng thái:")
			->setValue(0)
			  ->addMultiOptions(array('No','Yes'));
		
		//================= name ================
		$order = new Zend_Form_Element_Text('order'); //$name
		$order->setLabel('order:')
				->setDescription('Thu tu cua nhom');
		
		$sumbit = new Zend_Form_Element_Submit('submit');
		$sumbit->setValue('Save now');
		
		$this->addElements(
						array(
								$name,$avatar,$ranking,
							    $group_acp,$group_default,
							    $status,$order,$sumbit
							));
		
		if($arrParam['action'] == 'edit'){
			$name->setValue($item['name']);
			
			$current_avatar = new Zend_Form_Element_Hidden('current_avatar');
			$current_avatar->setValue($item['avatar']);
			
			$current_ranking = new Zend_Form_Element_Hidden('current_ranking');
			$current_ranking->setValue($item['ranking']);
			
			$group_acp->setValue($item['group_acp']);
			$group_default->setValue($item['group_default']);
			$status->setValue($item['status']);
			$order->setValue($item['order']);
			
			$this->addElements(
						array(
								$name,$avatar,$ranking,
							    $group_acp,$group_default,$current_ranking,$current_avatar,
							    $status,$order,$sumbit
							));
							
		}	
		
		
	}
	
	
	public function upload($arrParam = null){
	
		$upload = new Zendvn_File_Upload();
		$fileName = $upload->getFileName('avatar',false);
		if(!empty($fileName)){
			$dir_upload = FILE_PATH .'/group/avatar/';
			$arrData['avatar'] = $upload->upload('avatar', $dir_upload, array('rename'=>true));
			
			$img = Zendvn_File_Image::create($dir_upload . $arrData['avatar']);
			$img->resize(110,110)->save($dir_upload . 'img110x110/' . $arrData['avatar']);
			
			if($arrParam['action']=='edit'){
				
				$upload->remove($arrParam['current_avatar'],$dir_upload );
				$upload->remove($arrParam['current_avatar'],$dir_upload . 'img110x110/');
			}
		}
		
		$fileName = $upload->getFileName('ranking',false);
		if(!empty($fileName)){
			$dir_upload = FILE_PATH .'/group/ranking/';
			$arrData['ranking'] = $upload->upload('ranking', $dir_upload, array('rename'=>true));
			
			$img = Zendvn_File_Image::create($dir_upload . $arrData['ranking']);
			$img->resize(110,110)->save($dir_upload . 'img110x110/' . $arrData['ranking']);
			
			if($arrParam['action']=='edit'){
				$upload->remove($dir_upload . $arrParam['current_ranking']);
				$upload->remove($dir_upload . 'img110x110/' . $arrParam['current_ranking']);
			}
		}
		
		return $arrData;
	}
}






